package com.srpingde.hhh.application;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

/**
 * 
 * @author Yjw
 * @category 多点触控图片缩放监听
 * 
 */
public class MulitPointTouchListener implements OnTouchListener {
	Matrix matrix = new Matrix();
	Matrix savedMatrix = new Matrix();
	static Matrix defaultMatrix;
	
	float X1;
	float Y1;
	float X2;
	float Y2;
	
	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	int mode = NONE;

	PointF start = new PointF();
	PointF mid = new PointF();
	float dragX = 0;
	float dragY = 0;
	float oldDist = 1f;
	// ,defaultDist = 1f
	// float totalScale = 1f;
	boolean isDragableX = false;
	boolean isDragableY = false;
	boolean ischeck = false;

	// Rect rect = new Rect();
	static float[] dValues = new float[9];

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		ImageView view = (ImageView) v;
		// view.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);
		float newDist = 0f;
		float[] values = new float[9];
		// view.getDrawable().getBounds().
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			X1 = event.getX();
			Y1 = event.getY();
			if (null == defaultMatrix) {
				defaultMatrix = view.getImageMatrix();
				defaultMatrix.getValues(dValues);
				// rect = view.getDrawable().getBounds();
				// defaultMatrix.getValues(dValues);
			}
			view.setScaleType(ScaleType.MATRIX);
			matrix.set(view.getImageMatrix());
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			if (isDragableX || isDragableY)
				mode = DRAG;
			else
				mode = NONE;
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = spacing(event);
			if (oldDist > 10f) {
				savedMatrix.set(matrix);
				midPoint(mid, v);
				mode = ZOOM;
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			

			/*
			 * Matrix mMoveMatrix = view.getImageMatrix();
			 * mMoveMatrix.getValues(values); //获取图片描述的具体值：9个长度的int类型数组 Rect
			 * rect = view.getDrawable().getBounds(); float left = values[2];
			 * float right = left + rect.width() * values[0];
			 * //其实就是下面的width+left float width = rect.width() * values[0]; float
			 * height = rect.height() * values[0];
			 */
			X2 = event.getX();

			Y2 = event.getY();
			if (Math.abs(X1 - X2) < 6) {
				return false; // distance is smaller, as the click event
			}if (Math.abs(X1 - X2) > 60) {// onTouch real events
				if (mode == DRAG) {
					getValues(view, values);
					// matrix.getValues(values);
					Rect rect = view.getDrawable().getBounds();
					float left = values[2];
					float right = left + rect.width() * values[0]; // 其实就是下面的width+left
					float top = values[5];
					float bottom = top + rect.height() * values[0];
					if (isDragableX) {
						if (left > 0) {
							dragX = -left;
						}else if (right < view.getWidth()) {
							dragX = view.getWidth() - right;
						}else{
							dragX = 0;
						}
					}
					if (isDragableY) {
						if (top > 0) {
							dragY = -top;
						}else if (bottom < view.getHeight()) {
							dragY = view.getHeight() - bottom;
						}else{
							dragY = 0;
						}
					}
					matrix.postTranslate(dragX, dragY);
				}
					mode = NONE;
					// Rect curr = view.getDrawable().getBounds();
					getValues(view, values);
					/*
					 * String curr = ""; for(float n : currWidth){ curr += n+"\t"; }
					 */
					Log.d("width", "currwidth:" + values[0] + "  dWidth:"
							+ dValues[0]);
					if (values[0] <= dValues[0]) {
						// Log.d("size", "defaultSize:"+
						view.setScaleType(ScaleType.CENTER_INSIDE);
						matrix.set(defaultMatrix);
						isDragableX = false;
						isDragableY = false;
						// matrix.set(defaultMatrix);
						// matrix.preScale(1, 1, mid.x, mid.y);
						// totalScale = 1;
					} else {
						Rect rect = view.getDrawable().getBounds();
						float left = values[2];
						float right = left + rect.width() * values[0]; // 其实就是下面的width+left
						float top = values[5];
						float bottom = top + rect.height() * values[0];
						isDragableX = left < 0||right>view.getWidth();
						isDragableY = top < 0||bottom>view.getHeight();
						
					}
			}
			// matrix.reset();
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == DRAG) {

				matrix.set(savedMatrix);
				getValues(view, values);
				// matrix.getValues(values);
				Rect rect = view.getDrawable().getBounds();
				float left = values[2];
				float right = left + rect.width() * values[0]; // 其实就是下面的width+left
				float top = values[5];
				float bottom = top + rect.height() * values[0];
				// float width = rect.width() * values[0];
				// float height = rect.height() * values[0];
				if (isDragableX)
						dragX = (event.getX() - start.x);
				else
					dragX = 0;
				if (isDragableY)
						dragY = (event.getY() - start.y);
				else
					dragY = 0;
				matrix.postTranslate(dragX, dragY);
			} else if (mode == ZOOM) {
				newDist = spacing(event);
				if (newDist > 10f) {
					matrix.set(savedMatrix);
					float scale = newDist / oldDist;
					// scale = 0.8f;
					// totalScale = totalScale*scale;
					matrix.postScale(scale, scale, mid.x, mid.y);
					// Log.d("scale--------------", totalScale+"");
				}
			}
			break;
		}

		view.setImageMatrix(matrix);
		return true;
	}

	private void getValues(ImageView view, float[] values) {
		Matrix mMoveMatrix = view.getImageMatrix();
		mMoveMatrix.getValues(values);
		// Rect rect = view.getDrawable().getBounds();
	}

	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return (float) Math.sqrt(x * x + y * y);
	}

	private void midPoint(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

	private void midPoint(PointF point, View v) {
		float x = v.getRight() - v.getLeft();
		float y = v.getBottom() - v.getTop();
		point.set(x / 2, y / 2);
	}
}
